#include<bits/stdc++.h>

using namespace std;

using i64 = long long;
using u64 = unsigned long long;

struct Ga {
    string left, right;

    Ga(string left, string right) {
        this->left = left;
        this->right = right;
    }
} g[4] = {Ga("E", "S"), Ga("S", "BB"), Ga("B", "aB"), Ga("B", "b")};
/*
    E -> S ; r0
    S -> B B; r1
    B -> a B; r2
    B -> b; r3
 */                 // a     b     #       S    B
string table[][5] = {"s3", "s4", "error", "1", "2",
                     "error", "error", "acc", "error", "error",
                     "s3", "s4", "error", "error", "5",
                     "s3", "s4", "error", "error", "6",
                     "r3", "r3", "r3", "error", "error",
                     "r1", "r1", "r1", "error", "error",
                     "r2", "r2", "r2", "error", "error"};
string s;
int cnt_1 = 0;

void output(const vector<char> &a, int x) {
    cout << ++cnt_1 << "\t";

    vector<char> c;
    int cnt = 0;
    for (int i = 0; i < a.size(); i++) {
        if ( isalpha(a[i])) {
            c.push_back(a[i]);
        } else {
            cout << a[i];
            cnt++;
        }
    }

    cout << "\t\t" << "#";
    for (int i = 0; i < c.size(); i++) {
        cout << c[i];
    }
    cout << "\t\t";
    for (int i = x; i < s.size(); i++) {
        cout << s[i];
    }
    cout << " ";

}

int main() {

    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    // freopen("input.in", "r", stdin);
    unordered_map<char, int> p;
    p['a'] = 0, p['b'] = 1;
    p['#'] = 2, p['S'] = 3, p['B'] = 4;

    cin >> s;

    vector<char> st;
    st.push_back('0');

    int i = 0;
    while (true) {
        char t = st.back();
        assert(isdigit(t) == true);
        string y = table[t - '0'][p[s[i]]];
        if ( y[0] == 's' ) {
            output(st, i);
            y[0] = toupper(y[0]);
            cout << "\t\t" << y << "\n";
            st.push_back(s[i]);//先压符号
            st.push_back(y[1]);//在压状态

            i++;

        } else if ( y[0] == 'r' ) {//根据r? 归约
            output(st, i);
            int cnt = 0;
            Ga g1 = g[y[1] - '0'];//要归约的产生式
            while (cnt < 2 * g1.right.size()) {//弹出 2*right.size()
                st.pop_back();
                cnt++;
            }
            const char *c1 = g1.left.c_str();//产生式的左部
            string y1 = table[st.back() - '0'][p[c1[0]]];//goto表
            st.push_back(c1[0]);//先压符号 压入左部
            st.push_back(y1[0]);//在压状态 压入goto的状态

            cout << "\t\t" << y << "\t" << y1 << "\n";

        } else if ( y == "acc" ) {
            output(st, i);
            cout << "\t\t" << "acc" << "\n";
            break;
        } else {
            output(st, i);
            cout << "\t\t" << "error" << "\n";
            break;
        }
    }
    return 0;
}
/**************************************************************
    Problem: 4044
    User: 2701220528
    Language: C++
    Result: 正确
    Time:4 ms
    Memory:2392 kb
    Judger:judge.aliyun2
****************************************************************/